﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using FimetContoDeposito.BLL;
using FimetContoDeposito.BLL.Facades;
using GrayParrot.Core;
using GrayParrot.Web.Controls;
using Newtonsoft.Json.Linq;

namespace FimetContoDeposito.Controllers
{
    public class CommonController : ApiController
    {
        [HttpPost]
        public List<NameValue<string, int>> LoadColori()
        {
            List<NameValue<string, int>> retValue = null;

            using (AnagraficaColoriFacade m_cf = AnagraficaColoriFacade.Create())
            {
                var objs = m_cf.GetAll();

                if (objs.HasValue)
                {
                    if (objs.Value.Any())
                    {
                        retValue = objs.Value.Select(x => new NameValue<string, int>() { name = x.CodiceColore, value = x.IdColore }).ToList();
                    }
                }
            }

            return retValue;
        }

        [HttpGet]
        public List<NameValue<string, Guid>> LoadTipoOperazioni()
        {
            List<NameValue<string, Guid>> retValue = null;

            using (TipoOperazioneFacade m_cf = TipoOperazioneFacade.Create())
            {
                var objs = m_cf.GetAll();

                if (objs.HasValue)
                {
                    if (objs.Value.Any())
                    {
                        retValue = objs.Value.Select(x => new NameValue<string, Guid>() { name = x.Descrizione, value = x.Id }).ToList();
                    }
                }
            }

            return retValue;
        }

        [HttpGet]
        public List<NameValue<string, Guid>> LoadTipoMovimento()
        {
            List<NameValue<string, Guid>> retValue = null;

            using (TipoMovimentoFacade m_cf = TipoMovimentoFacade.Create())
            {
                var objs = m_cf.GetAll();

                if (objs.HasValue)
                {
                    if (objs.Value.Any())
                    {
                        retValue = objs.Value.Select(x => new NameValue<string, Guid>() { name = x.Descrizione, value = x.Id }).ToList();
                    }
                }
            }

            return retValue;
        }

        [HttpPost]
        public SelectFilterResult<IEnumerable<NameValue<string, Guid>>> LoadTipoMovimento(SelectFilterParams data)
        {
            SelectFilterResult<IEnumerable<NameValue<string, Guid>>> retValue = null;

            List<NameValue<string, Guid>> m_objs = this.LoadTipoMovimento();

            if (m_objs != null)
            {
                retValue = new SelectFilterResult<IEnumerable<NameValue<string, Guid>>>();
                retValue.Results = m_objs;
            }

            return retValue;
        }

        [HttpPost]
        public SelectFilterResult<IEnumerable<NameValue<string, string>>> LoadPriorita(SelectFilterParams data)
        {
            SelectFilterResult<IEnumerable<NameValue<string, string>>> retValue = null;

            retValue = new SelectFilterResult<IEnumerable<NameValue<string, string>>>();

            List<NameValue<string, string>> m_data = new List<NameValue<string, string>>();
            m_data.Add(new NameValue<string, string>() { name = "Si", value = "true" });
            m_data.Add(new NameValue<string, string>() { name = "No", value = "false" });

            retValue.Results = m_data;

            return retValue;
        }

        [HttpGet]
        public List<NameValue<string, Guid>> LoadArticolo()
        {
            List<NameValue<string, Guid>> retValue = null;

            using (AnagraficaArticoliFacade m_cf = AnagraficaArticoliFacade.Create())
            {
                var objs = m_cf.GetAll();

                if (objs.HasValue)
                {
                    if (objs.Value.Any())
                    {
                        retValue = objs.Value.Select(x => new NameValue<string, Guid>() { name = x.Descrizione, value = x.Id }).ToList();
                    }
                }
            }

            return retValue;
        }

        [HttpGet]
        public List<NameValue<string, Guid>> LoadCustomers()
        {
            List<NameValue<string, Guid>> retValue = null;

            using (CustomersFacade m_cf = CustomersFacade.Create())
            {
                var objs = m_cf.GetAll();

                if (objs.HasValue)
                {
                    if (objs.Value.Any())
                    {
                        retValue = objs.Value.Select(x => new NameValue<string, Guid>() { name = x.RagioneSociale, value = x.Id }).ToList();
                    }
                }
            }

            return retValue;
        }

        [HttpPost]
        public List<NameValue<string, Guid>> LoadCustomersByFornitore(JObject data)
        {
            List<NameValue<string, Guid>> retValue = null;

            dynamic json = data;

            if (json.parameters[0] != null && json.parameters[0] != "")
            {
                Guid m_idfornitore = Guid.Parse(json.parameters[0].ToString());

                using (FornitoriFacade m_cf = FornitoriFacade.Create())
                {
                    var objs = m_cf.Get(m_idfornitore, "FCD_Customers");

                    if (objs.HasValue)
                    {
                        if (objs.Value.FCD_Customers.Any())
                        {
                            retValue = objs.Value.FCD_Customers.Select(x => new NameValue<string, Guid>() { name = x.RagioneSociale, value = x.Id }).OrderBy(x=>x.name).ToList();
                        }
                    }
                }
            }

            return retValue;
        }

        [HttpPost]
        public SelectFilterResult<IEnumerable<NameValue<string, string>>> LoadCustomersDDL(SelectFilterParams data)
        {
            SelectFilterResult<IEnumerable<NameValue<string, string>>> retValue = null;

            using (CustomersFacade m_cf = CustomersFacade.Create())
            {
                var objs = m_cf.GetAll();

                if (objs.HasValue)
                {
                    if (objs.Value.Any())
                    {
                        retValue = new SelectFilterResult<IEnumerable<NameValue<string, string>>>();
                        IEnumerable<NameValue<string, string>> objs2 = null;
                        if ((data == null) || (string.IsNullOrWhiteSpace(data.SearchTerm)))
                        {
                            objs2 = objs.Value.Select(x => new NameValue<string, string>() { name = x.RagioneSociale, value = x.Id.ToString() });
                        }
                        else
                        {
                            objs2 = objs.Value.Select(x => new NameValue<string, string>() { name = x.RagioneSociale, value = x.Id.ToString() }).Where(x => x.name.ToLower().Contains(data.SearchTerm.ToLower()));
                        }

                        if ((data != null) && (data.PageNumber != 0) && (data.PageSize != 0))
                        {
                            retValue.Results = objs2.Skip((data.PageNumber - 1) * data.PageSize).Take(data.PageSize).ToList();
                        }
                        else
                        {
                            retValue.Results = objs2.ToList();
                        }


                        retValue.Total = objs2.Count();
                    }
                }
            }

            return retValue;
        }

        [HttpPost]
        public SelectFilterResult<IEnumerable<NameValue<string, string>>> LoadFornitoriDDL(SelectFilterParams data)
        {
            SelectFilterResult<IEnumerable<NameValue<string, string>>> retValue = null;

            using (FornitoriFacade m_cf = FornitoriFacade.Create())
            {
                var objs = m_cf.GetAll();

                if (objs.HasValue)
                {
                    if (objs.Value.Any())
                    {
                        retValue = new SelectFilterResult<IEnumerable<NameValue<string, string>>>();
                        IEnumerable<NameValue<string, string>> objs2 = null;
                        if ((data == null) || (string.IsNullOrWhiteSpace(data.SearchTerm)))
                        {
                            objs2 = objs.Value.Select(x => new NameValue<string, string>() { name = x.Descrizione, value = x.Id.ToString() });
                        }
                        else
                        {
                            objs2 = objs.Value.Select(x => new NameValue<string, string>() { name = x.Descrizione, value = x.Id.ToString() }).Where(x => x.name.ToLower().Contains(data.SearchTerm.ToLower()));
                        }

                        if ((data != null) && (data.PageNumber != 0) && (data.PageSize != 0))
                        {
                            retValue.Results = objs2.Skip((data.PageNumber - 1) * data.PageSize).Take(data.PageSize).ToList();
                        }
                        else
                        {
                            retValue.Results = objs2.ToList();
                        }

                        retValue.Total = objs2.Count();
                    }
                }
            }

            return retValue;
        }

        [HttpGet]
        public List<NameValue<string, Guid>> LoadFornitori()
        {
            List<NameValue<string, Guid>> retValue = null;

            using (FornitoriFacade m_cf = FornitoriFacade.Create())
            {
                var objs = m_cf.GetAll();

                if (objs.HasValue)
                {
                    if (objs.Value.Any())
                    {
                        retValue = objs.Value.Select(x => new NameValue<string, Guid>() { name = x.Descrizione, value = x.Id }).ToList();
                    }
                }
            }

            return retValue;
        }

        [HttpGet]
        public List<NameValue<string, Guid>> LoadTipoDocumento()
        {
            List<NameValue<string, Guid>> retValue = null;

            using (TipologiaDocumentoFacade m_cf = TipologiaDocumentoFacade.Create())
            {
                var objs = m_cf.GetAll();

                if (objs.HasValue)
                {
                    if (objs.Value.Any())
                    {
                        retValue = objs.Value.Select(x => new NameValue<string, Guid>() { name = x.Descrizione, value = x.Id }).ToList();
                    }
                }
            }

            return retValue;
        }

        [HttpPost]
        public SelectFilterResult<IEnumerable<NameValue<string, Guid>>> LoadTipoDocumento(SelectFilterParams data)
        {
            SelectFilterResult<IEnumerable<NameValue<string, Guid>>> retValue = null;

            using (TipologiaDocumentoFacade m_cf = TipologiaDocumentoFacade.Create())
            {
                var objs = m_cf.GetAll();

                if (objs.HasValue)
                {
                    retValue = new SelectFilterResult<IEnumerable<NameValue<string, Guid>>>();

                    if (objs.Value.Any())
                    {
                        retValue.Results = objs.Value.Select(x => new NameValue<string, Guid>() { name = x.Descrizione, value = x.Id }).ToList();
                    }
                }
            }

            return retValue;
        }

        [HttpGet]
        public List<NameValue<string, Guid>> LoadMagazzini()
        {
            List<NameValue<string, Guid>> retValue = null;

            using (MagazziniFacade m_cf = MagazziniFacade.Create())
            {
                var objs = m_cf.GetAll();

                if (objs.HasValue)
                {
                    if (objs.Value.Any())
                    {
                        retValue = objs.Value.Select(x => new NameValue<string, Guid>() { name = x.Nome, value = x.Id }).ToList();
                    }
                }
            }

            return retValue;
        }

        [HttpPost]
        public string GetEnumDescription(JObject data)
        {
            string retValue = string.Empty;

            dynamic json = data;
            int m_id = json.value;

            retValue = FCDHelpers.GetEnumDescription((TipoMovimento)m_id);

            return retValue;
        }
    }
}